/*
https://leetcode-cn.com/problems/integer-replacement/solution/zheng-shu-ti-huan-by-leetcode-solution-swef/
 */
public class Solution397 {
    public int integerReplacement(int n) {
        int c=0;
        while (n!=1){
            c++;
            if (n%2==0){
                n=n/2;
            }else if (n==3){
                n=2;
            }else if (n%4==1){
                n=n-1;
            }else if (n%4==3){
                if (n==Integer.MAX_VALUE){
                    n=n/2;
                    c++;
                }
                n=n+1;
            }
        }
        return c;
    }

    public static void main(String[] args) {
        System.out.println(new Solution397().integerReplacement(7));
    }
}
